<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单类型，1为同城订单，2为城际订单" prop="orderType">
      <el-input v-model="dataForm.orderType" placeholder="订单类型，1为同城订单，2为城际订单"></el-input>
    </el-form-item>
    <el-form-item label="取件类型，1为网点自寄，2为上门取件" prop="pickupType">
      <el-input v-model="dataForm.pickupType" placeholder="取件类型，1为网点自寄，2为上门取件"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="客户id" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="客户id"></el-input>
    </el-form-item>
    <el-form-item label="收件人省份id" prop="receiverProvinceId">
      <el-input v-model="dataForm.receiverProvinceId" placeholder="收件人省份id"></el-input>
    </el-form-item>
    <el-form-item label="收件人城市id" prop="receiverCityId">
      <el-input v-model="dataForm.receiverCityId" placeholder="收件人城市id"></el-input>
    </el-form-item>
    <el-form-item label="收件人区县id" prop="receiverCountyId">
      <el-input v-model="dataForm.receiverCountyId" placeholder="收件人区县id"></el-input>
    </el-form-item>
    <el-form-item label="收件人详细地址" prop="receiverAddress">
      <el-input v-model="dataForm.receiverAddress" placeholder="收件人详细地址"></el-input>
    </el-form-item>
    <el-form-item label="收件人地址id" prop="receiverAddressId">
      <el-input v-model="dataForm.receiverAddressId" placeholder="收件人地址id"></el-input>
    </el-form-item>
    <el-form-item label="收件人姓名" prop="receiverName">
      <el-input v-model="dataForm.receiverName" placeholder="收件人姓名"></el-input>
    </el-form-item>
    <el-form-item label="收件人电话" prop="receiverPhone">
      <el-input v-model="dataForm.receiverPhone" placeholder="收件人电话"></el-input>
    </el-form-item>
    <el-form-item label="发件人省份id" prop="senderProvinceId">
      <el-input v-model="dataForm.senderProvinceId" placeholder="发件人省份id"></el-input>
    </el-form-item>
    <el-form-item label="发件人城市id" prop="senderCityId">
      <el-input v-model="dataForm.senderCityId" placeholder="发件人城市id"></el-input>
    </el-form-item>
    <el-form-item label="发件人区县id" prop="senderCountyId">
      <el-input v-model="dataForm.senderCountyId" placeholder="发件人区县id"></el-input>
    </el-form-item>
    <el-form-item label="发件人详细地址" prop="senderAddress">
      <el-input v-model="dataForm.senderAddress" placeholder="发件人详细地址"></el-input>
    </el-form-item>
    <el-form-item label="发件人地址id" prop="senderAddressId">
      <el-input v-model="dataForm.senderAddressId" placeholder="发件人地址id"></el-input>
    </el-form-item>
    <el-form-item label="发件人姓名" prop="senderName">
      <el-input v-model="dataForm.senderName" placeholder="发件人姓名"></el-input>
    </el-form-item>
    <el-form-item label="发件人电话" prop="senderPhone">
      <el-input v-model="dataForm.senderPhone" placeholder="发件人电话"></el-input>
    </el-form-item>
    <el-form-item label="订单当前所属网点" prop="currentAgencyId">
      <el-input v-model="dataForm.currentAgencyId" placeholder="订单当前所属网点"></el-input>
    </el-form-item>
    <el-form-item label="付款方式,1.预结2到付" prop="paymentMethod">
      <el-input v-model="dataForm.paymentMethod" placeholder="付款方式,1.预结2到付"></el-input>
    </el-form-item>
    <el-form-item label="付款状态,1.未付2已付" prop="paymentStatus">
      <el-input v-model="dataForm.paymentStatus" placeholder="付款状态,1.未付2已付"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="预计到达时间" prop="estimatedArrivalTime">
      <el-input v-model="dataForm.estimatedArrivalTime" placeholder="预计到达时间"></el-input>
    </el-form-item>
    <el-form-item label="距离，单位：公里" prop="distance">
      <el-input v-model="dataForm.distance" placeholder="距离，单位：公里"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderType: '',
          pickupType: '',
          createTime: '',
          memberId: '',
          receiverProvinceId: '',
          receiverCityId: '',
          receiverCountyId: '',
          receiverAddress: '',
          receiverAddressId: '',
          receiverName: '',
          receiverPhone: '',
          senderProvinceId: '',
          senderCityId: '',
          senderCountyId: '',
          senderAddress: '',
          senderAddressId: '',
          senderName: '',
          senderPhone: '',
          currentAgencyId: '',
          paymentMethod: '',
          paymentStatus: '',
          amount: '',
          estimatedArrivalTime: '',
          distance: '',
          status: ''
        },
        dataRule: {
          orderType: [
            { required: true, message: '订单类型，1为同城订单，2为城际订单不能为空', trigger: 'blur' }
          ],
          pickupType: [
            { required: true, message: '取件类型，1为网点自寄，2为上门取件不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          memberId: [
            { required: true, message: '客户id不能为空', trigger: 'blur' }
          ],
          receiverProvinceId: [
            { required: true, message: '收件人省份id不能为空', trigger: 'blur' }
          ],
          receiverCityId: [
            { required: true, message: '收件人城市id不能为空', trigger: 'blur' }
          ],
          receiverCountyId: [
            { required: true, message: '收件人区县id不能为空', trigger: 'blur' }
          ],
          receiverAddress: [
            { required: true, message: '收件人详细地址不能为空', trigger: 'blur' }
          ],
          receiverAddressId: [
            { required: true, message: '收件人地址id不能为空', trigger: 'blur' }
          ],
          receiverName: [
            { required: true, message: '收件人姓名不能为空', trigger: 'blur' }
          ],
          receiverPhone: [
            { required: true, message: '收件人电话不能为空', trigger: 'blur' }
          ],
          senderProvinceId: [
            { required: true, message: '发件人省份id不能为空', trigger: 'blur' }
          ],
          senderCityId: [
            { required: true, message: '发件人城市id不能为空', trigger: 'blur' }
          ],
          senderCountyId: [
            { required: true, message: '发件人区县id不能为空', trigger: 'blur' }
          ],
          senderAddress: [
            { required: true, message: '发件人详细地址不能为空', trigger: 'blur' }
          ],
          senderAddressId: [
            { required: true, message: '发件人地址id不能为空', trigger: 'blur' }
          ],
          senderName: [
            { required: true, message: '发件人姓名不能为空', trigger: 'blur' }
          ],
          senderPhone: [
            { required: true, message: '发件人电话不能为空', trigger: 'blur' }
          ],
          currentAgencyId: [
            { required: true, message: '订单当前所属网点不能为空', trigger: 'blur' }
          ],
          paymentMethod: [
            { required: true, message: '付款方式,1.预结2到付不能为空', trigger: 'blur' }
          ],
          paymentStatus: [
            { required: true, message: '付款状态,1.未付2已付不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          estimatedArrivalTime: [
            { required: true, message: '预计到达时间不能为空', trigger: 'blur' }
          ],
          distance: [
            { required: true, message: '距离，单位：公里不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/oms/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderType = data.order.orderType
                this.dataForm.pickupType = data.order.pickupType
                this.dataForm.createTime = data.order.createTime
                this.dataForm.memberId = data.order.memberId
                this.dataForm.receiverProvinceId = data.order.receiverProvinceId
                this.dataForm.receiverCityId = data.order.receiverCityId
                this.dataForm.receiverCountyId = data.order.receiverCountyId
                this.dataForm.receiverAddress = data.order.receiverAddress
                this.dataForm.receiverAddressId = data.order.receiverAddressId
                this.dataForm.receiverName = data.order.receiverName
                this.dataForm.receiverPhone = data.order.receiverPhone
                this.dataForm.senderProvinceId = data.order.senderProvinceId
                this.dataForm.senderCityId = data.order.senderCityId
                this.dataForm.senderCountyId = data.order.senderCountyId
                this.dataForm.senderAddress = data.order.senderAddress
                this.dataForm.senderAddressId = data.order.senderAddressId
                this.dataForm.senderName = data.order.senderName
                this.dataForm.senderPhone = data.order.senderPhone
                this.dataForm.currentAgencyId = data.order.currentAgencyId
                this.dataForm.paymentMethod = data.order.paymentMethod
                this.dataForm.paymentStatus = data.order.paymentStatus
                this.dataForm.amount = data.order.amount
                this.dataForm.estimatedArrivalTime = data.order.estimatedArrivalTime
                this.dataForm.distance = data.order.distance
                this.dataForm.status = data.order.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/oms/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderType': this.dataForm.orderType,
                'pickupType': this.dataForm.pickupType,
                'createTime': this.dataForm.createTime,
                'memberId': this.dataForm.memberId,
                'receiverProvinceId': this.dataForm.receiverProvinceId,
                'receiverCityId': this.dataForm.receiverCityId,
                'receiverCountyId': this.dataForm.receiverCountyId,
                'receiverAddress': this.dataForm.receiverAddress,
                'receiverAddressId': this.dataForm.receiverAddressId,
                'receiverName': this.dataForm.receiverName,
                'receiverPhone': this.dataForm.receiverPhone,
                'senderProvinceId': this.dataForm.senderProvinceId,
                'senderCityId': this.dataForm.senderCityId,
                'senderCountyId': this.dataForm.senderCountyId,
                'senderAddress': this.dataForm.senderAddress,
                'senderAddressId': this.dataForm.senderAddressId,
                'senderName': this.dataForm.senderName,
                'senderPhone': this.dataForm.senderPhone,
                'currentAgencyId': this.dataForm.currentAgencyId,
                'paymentMethod': this.dataForm.paymentMethod,
                'paymentStatus': this.dataForm.paymentStatus,
                'amount': this.dataForm.amount,
                'estimatedArrivalTime': this.dataForm.estimatedArrivalTime,
                'distance': this.dataForm.distance,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
